package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.GuaranteeBill;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.query.AccidentQueryObject;
import cn.wolfcode.crm.query.AccidentQueryObjectForGuaranteeBill;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 21:45
 **/
@Service
public class AccidentServiceImpl implements IAccidentService {
    @Autowired
    private AccidentMapper mapper;


    public void save(Accident entity) {
        //获取当前登录的用户(录入人)
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        entity.setInputUser(employee);
        //添加报案信息的时候状态值默认为"待验证(0)"
        entity.setStatus(0);
        //把表单对象封装到Accident中
        GuaranteeBill guaranteeBill = new GuaranteeBill();
        Long clientId = entity.getClient().getId();


        Long guaranteeBillId = mapper.queryGtbIdByClientId(clientId);
        guaranteeBill.setId(guaranteeBillId);
        entity.setGuaranteeBill(guaranteeBill);

        mapper.insert(entity);
    }

    public void update(Accident entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public void delete(long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public Accident get(long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Accident> list() {
        return mapper.selectAll();
    }

    @Override
    public PageResult query(AccidentQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Accident> list = mapper.queryForList(qo);
        return new PageResult(total, list);
    }

    public PageResult query1(AccidentQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Accident> list = mapper.queryForList1(qo);
        return new PageResult(total, list);
    }

    public PageResult query2(AccidentQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Accident> list = mapper.queryForList2(qo);
        return new PageResult(total, list);
    }

    public PageResult query3(AccidentQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Accident> list = mapper.queryForList3(qo);
        return new PageResult(total, list);
    }

    public void changeStatus(Long id) {
        mapper.changeStatus(id);
    }

      @Override
    public void changeStatus1(Long id) {
        mapper.changeStatus1(id);
    }

    public void changeStatus2(Long id) {
        mapper.changeStatus2(id);
    }

    public void changeStatus3(Long id) {
        mapper.changeStatus3(id);
    }

    public void changeStatus4(Long id) {
        mapper.changeStatus4(id);
    }
    public void changeStatus5(Long id) {
        mapper.changeStatus5(id);
    }

    @Override
    public boolean hasCom(Long paramLong) {
        Integer count = mapper.hasCom(paramLong);
        return count!=0;

    }

    public List<Map<String, String>> selectForDuty(String duty) {
        return mapper.selectForDuty(duty);
    }

    public PageResult selectGuaranteeBillInfo(AccidentQueryObjectForGuaranteeBill qo) {
        int total = mapper.queryForCountForGuaranteeBill(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<GuaranteeBill> list = mapper.queryForListForGuaranteeBill(qo);
        return new PageResult(total, list);

    }

}
